package zhieasy.cn.xpu_helper.controller.initiation;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.common.FreshManId;
import zhieasy.cn.model.common.StudentUser;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.service.common.StudentUserService;
import zhieasy.cn.service.common.WechatUserRoleService;
import zhieasy.cn.service.common.WechatUserService;
import zhieasy.cn.service.initiation.FreshmanIdService;

import java.util.Objects;

/**
 * @author DaHang
 * @date 2020/9/5 0:05
 */

@Api(tags = "intitiation模块 新生校验信息接口")
@RestController
@RequestMapping("/initiation/freshman")
public class FreshmanController {

    private static final Logger logger = LoggerFactory.getLogger(FreshmanController.class);
    @Autowired
    FreshmanIdService freshmanIdService;

    @Autowired
    WechatUserService wechatUserService;

    @Autowired
    StudentUserService StudentUserService;

    @Autowired
    WechatUserRoleService wechatUserRoleService;
    @ApiOperation(value = "输入新生编号获取个人验证信息")
    @RequestMapping(value = "/register/{freshmanId}",method = RequestMethod.GET)
    public ResultVO getFreshmanInfo(@ApiParam("新生编号")@PathVariable("freshmanId")String freshmanId){
        FreshManId freshmanInfo = freshmanIdService.getById(freshmanId);
        //对freshman中的信息做模糊化处理
        if (freshmanInfo == null){
            return ResultUtils.error("编号错误");
        }
        FreshManId freshManIdResult = new FreshManId();
        freshManIdResult.setName(freshmanInfo.getName());
        logger.info("当前用户查询到的数据是{},{}",freshmanInfo,freshManIdResult);
//        //如果姓名不是空
//        if (freshmanInfo.getName() != null && !freshmanInfo.getName().isEmpty()
//            && freshmanInfo.getTelephone() != null && !freshmanInfo.getTelephone().isEmpty()){
//            String name = freshmanInfo.getName();
//            //模糊化处理
//            StringBuilder nameFuzzy = new StringBuilder(name);
//            //看看名字多长
//            int length = nameFuzzy.length();
//            String startNum = "";
//            for (int i = 0; i < length-1; i++) {
//                startNum += "*";
//            }
//            nameFuzzy.replace(0,nameFuzzy.length()-1,startNum);
//            //封装一下
//            freshManIdResult = new FreshManId();
//            freshManIdResult.setName(nameFuzzy.toString());
////            freshManIdResult.setTelephone(telephoneFuzzy.toString());
//        }
        return ResultUtils.success(freshManIdResult);
    }

    @ApiOperation(value = "校验新生")
    @RequestMapping(value = "/register/{freshmanId}",method = RequestMethod.PUT)
    public ResultVO checkFreshmanInfo(@ApiParam("新生信息类")@RequestBody FreshManId checkFreshManIdInfo,
                                      @ApiParam("新生编号")@PathVariable("freshmanId")String freshmanId){
        //获取到需要验证的的人的信息
        FreshManId freshmanInfo = freshmanIdService.getById(freshmanId);
        if (freshmanInfo == null){
            return ResultUtils.error("编号有问题");
        }
        if (!freshmanInfo.getName().equals(checkFreshManIdInfo.getName()) &&
            !checkFreshManIdInfo.getCollageId().equals(freshmanInfo.getCollageId())){
            return ResultUtils.error("验证失败");
        }

        //判断是否注册过
        Boolean flag = StudentUserService.isHaveStudentUser(freshmanInfo.getOpenId());
        if(!flag){
            return ResultUtils.error("该用户已经绑定,如信息无误联系管理员");
        }
        StudentUser studentUser = new StudentUser();
        studentUser.setCollegeId(freshmanInfo.getCollageId());
        studentUser.setOpenId(WechatUser.getWechatUserOpenIdFromSecurityContext());
        studentUser.setCreateTime(System.currentTimeMillis());
        studentUser.setGradeId(freshmanInfo.getMajorClass());
        studentUser.setStudentId(freshmanInfo.getId());
        studentUser.setStudentDormitory(freshmanInfo.getDormitory());
        studentUser.setGradeId(freshmanInfo.getMajorClass());
        studentUser.setMajorId(freshmanInfo.getMajorId());
        studentUser.setUpdateTime(System.currentTimeMillis());
        //设置1为新生认证，但不是学生认证
        studentUser.setStudentStatus(1);
        //这里关联一下学生的信息和用户的open_id
        freshmanInfo.setOpenId(WechatUser.getWechatUserOpenIdFromSecurityContext());
        boolean b = freshmanIdService.updateById(freshmanInfo);
        if (!b) {
            return ResultUtils.error("更新新生信息失败");
        }
        boolean save = StudentUserService.save(studentUser);
        if (!save) {
            return ResultUtils.error("新增新生信息失败");
        }
        //给学生设置一下权限
//        wechatUserRoleService.setStudentRole();
        return ResultUtils.success();
    }
}
